package com.stormragetech.teamwork.module.user.spring.dao.impl;

import com.stormragetech.common.dao.MysqlBaseDao;
import com.stormragetech.common.exception.StormragetechException;
import com.stormragetech.teamwork.common.constant.Constants;
import com.stormragetech.teamwork.module.user.entity.User;
import com.stormragetech.teamwork.module.user.spring.dao.UserDao;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Log4j2
@Repository
public class UserDaoImpl extends MysqlBaseDao implements UserDao {

    @Autowired
    public UserDaoImpl(NamedParameterJdbcTemplate jdbcTemplate) {
        super(jdbcTemplate);
    }

    @Override
    public int create(final User entity) throws StormragetechException {

        final String query = "INSERT INTO user " +
                "(username, password, salt, realname, reg_date, locked) " +
                "VALUES" +
                "(:username, :password, :salt, :realname, :regDate, :locked)";

        try {
            return create(query, entity);

        } catch (Exception e) {
            throw new StormragetechException(Constants.DAO_ERR_CODE, e.getMessage(), e);
        }
    }

    @Override
    public User queryById(final Map<String, Object> map) throws StormragetechException {

        final String query = "SELECT * " +
                "FROM user " +
                "WHERE id = :id";
        try {
            return queryObject(query, User.class, map);

        } catch (Exception e) {
            throw new StormragetechException(Constants.DAO_ERR_CODE, e.getMessage(), e);
        }
    }

    @Override
    public User queryByName(final Map<String, Object> map) throws StormragetechException {

        final String query = "SELECT * " +
                "FROM user " +
                "WHERE username = :username";
        try {
            log.info(queryObject(query, User.class, map));
            return queryObject(query, User.class, map);
        } catch (Exception e) {
            throw new StormragetechException(Constants.DAO_ERR_CODE, e.getMessage(), e);
        }
    }

    @Override
    public List<String> queryPermByUser(final Map<String, Object> map) throws StormragetechException {

        final String query = "SELECT e.sign " +
                "FROM user AS a, " +
                "user_position AS b, " +
                "position_role AS c, " +
                "auth_role AS d, " +
                "authority AS e " +
                "WHERE a.id = b.user_id " +
                "AND b.position_id = c.position_id " +
                "AND c.role_id = d.role_id " +
                "AND d.auth_id = e.id " +
                "AND a.id = :id";

        try {
            return queryList(query, String.class, map);
        } catch (Exception e) {
            throw new StormragetechException(Constants.DAO_ERR_CODE, e.getMessage(), e);
        }
    }
}
